package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
@SuppressWarnings("all") //需要传入spuId 如果是all则忽略警告信息
public class ESManagerServiceImpl implements ESManagerService {
    @Autowired
    private ESManagerMapper esManagerMapper;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 创建索引库结构
     */
    @Override
    public void createMappingAndIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    /**
     * 导入全部数据到es
     */
    @Override
    public void impoertAll() {
        //通过查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }

        //将skuList集合转为json
        String jsonSkuList = JSON.toJSONString(skuList);
        //将jsonSkuList在转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);
        //循环skuInfoList,将里面的spec属性由字符串转为map集合
        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息转为map存储
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }

        //导入到索引库中
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据spuId查询skuList集合,添加到索引库
     *
     * @param spuId
     */
    @Override
    public void importDataBySpuId(String spuId) {
        //通过feign发起远程调用
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() < 0) {
            throw new RuntimeException("当前数据没有被查询到,无法导入索引库");
        }
        //将skuList转为json
        String jsonSkuList = JSON.toJSONString(skuList);
        //将skuList转为skuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        //循环skuInfo ，将spec字符串转为map存入到索引库
        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //添加到索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据spuId查询skuList集合,删除索引
     *
     * @param spuId
     */
    @Override
    public void delDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if(skuList==null || skuList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //循环遍历skuList 从索引库中删除
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }
}
